use core::marker::PhantomData;

use uefi_ffi::services::boot::task::{TPL_CALLBACK, TPL_NOTIFY};

use crate::EFI_BOOT_SERVICES;

pub struct Task {
    phantom: PhantomData<usize>,
}

#[repr(C)]
pub enum Tpl {
    Application = 2,
    Callback = 3,
    Notify = 4,
}
impl Into<usize> for Tpl {
    fn into(self) -> usize {
        1 << (self as usize)
    }
}
impl From<usize> for Tpl {
    fn from(val: usize) -> Self {
        match val {
            TPL_CALLBACK => Tpl::Callback,
            TPL_NOTIFY => Tpl::Notify,
            _ => Tpl::Application,
        }
    }
}

impl Task {
    /// 提升任务优先级别，并且返回之前的优先级。
    pub fn raise_tpl(new_tpl: Tpl) -> Tpl {
        unsafe { ((*EFI_BOOT_SERVICES).raise_tpl)(new_tpl.into()) }.into()
    }
    /// 将任务的优先级恢复到原先的值
    ///
    /// + old_tpl: 用来恢复的原有的优先级别，需要和 raise_tpl 的调用匹配。
    pub fn restore_tpl(old_tpl: Tpl) {
        unsafe { ((*EFI_BOOT_SERVICES).restore_tpl)(old_tpl.into()) }
    }
}
